package com.riaevolution.components {
	import com.riaevolution.utils.ColorUtils;
	
	import mx.core.UIComponent;

	public class ColorDisplay extends UIComponent {
		public function ColorDisplay() {
			super();
		}

		private var _currentColorChanged:Boolean = false;

		private var _currentColor:Object = "#000000";

		public function set currentColor(value:Object):void {
			if (_currentColor != value) {
				_currentColor = value;
				_currentColorChanged = true;

				if (!_currentColor) {
					_currentColor = "#000000";
				}
				this.toolTip = (_currentColor is Number ? 
					ColorUtils.getNumberAsHexString(_currentColor as Number,6):
					_currentColor.toString());
				
				invalidateDisplayList();
			}
		}

		public function get currentColor():Object {
			return _currentColor;
		}

		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
			super.updateDisplayList(unscaledWidth, unscaledHeight);

			var color:Number = _currentColor is String ? ColorUtils.createColor(_currentColor.toString()) : _currentColor as Number;
			graphics.clear();
			graphics.lineStyle(1, 0xdedede);
			graphics.beginFill(color);
			graphics.moveTo(0, 0);

			graphics.lineTo(unscaledWidth, 0);
			graphics.lineTo(unscaledWidth, unscaledHeight);
			graphics.lineTo(0, unscaledHeight);
			graphics.lineTo(0, 0);

			graphics.endFill();
		}

		override protected function measure():void {
			super.measure();

			measuredMinWidth = 12;
			measuredMinHeight = 12;
			measuredWidth = 60;
			measuredHeight = 22;
		}
	}
}